// ACW

// Copy this text and paste it into a new Arduino Sketch

// PWM_Test 8/1/09 Joe Pardue
// this is tha same as ASCII_To_Integer except
// for one line adding analogWrite to loop()

// Duplicates (somewhat) the C standard libray
// function atoi() algorithm using Arduino. 
// Note that there is no filtering of the input
// so if you enter something other than an integer
// value between 0! and 65535!, well, good luck.

#include "libACW001.h"

int main(void)
{
	init();		// Initialize the private stuff
	setup();	// Setup the public stuff

	for(;;)		// Call loop() forever
		loop();

	return 0;	// You never get here

}



int value = 0;  // variable to keep the actual value 
int ledpin = 9; // light connected to digital pin 9

int myInput = 0;
int myNum[6];
int myCount = 0;
int i = 0;
int n = 0;

void setup()
{
   //Serial.begin(9600);				// TAW 
   //Serial.println("PWM Test");  		// TAW
   serialBegin(9600);
   printf("PWM Test\n");
}

void loop()
{
  // get characters until receiving '!' 
  while( myInput != '!' ) getNum();
  
  // convert end-of-number character '!' to 0
  myInput = 0;
  myNum[--myCount] = 0;
  
  // convert ASCII string to integer
  ATOI();
  
  // control the brightness of an LED
  analogWrite(ledpin, n); 
     
  // clean up and do it all again
  clearAll();
}

// Put serial characters in a character array
void getNum()
{   
  //if(Serial.available())			// TAW
  if (serialAvailable())
  {  
     //myInput = Serial.read(); 	// TAW
	 myInput = serialRead();
     // put the character in the array
     myNum[myCount++] = myInput;  
  }    
}

void ATOI()
{    
  // algorithm from atoi() in C standard library   
  n = 0;
  for(i = 0; myNum[i] >= '0' && myNum[i] <= '9'; ++i)
    n = 10 * n + (myNum[i] - '0');

  // show the number
  //Serial.print("You sent: ");			// TAW
  //Serial.println((unsigned int)n,DEC);  // TAW
  printf("You sent: %u\n", (unsigned int)n); 
}

void clearAll()
{
  myCount = 0;
  for(i = 0; i < 6; i++)
  {
    myNum[i] = 0;
  }
  //Serial.flush();  			// TAW
  serialFlush;
}
